#include "NetPointProtocol.h"

NetPointProtocol::NetPointProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::NetPointProtocol;
}

QList<Package> NetPointProtocol::parseData(QByteArray data)
{
    QList<Package> packageList;

    QString strData = data;
    QStringList strDataList = strData.split("\r\n");
    quint16 address=0,value=0,roomNum=0;
    QByteArray body;
    QString floor="0";
    QString extNumber;
    QString doorNum;
    body.clear();
    //quint8 takedata=data.at(0);
    qDebug()<<"netprotocol running";
    for(int num=0;num<strDataList.size()-1;num++)
    {
    if(strDataList.at(num) == "SetPointValue")
    {
       for(int i=1+num; i<=strDataList.size()-1; i++)
       {
           int index = strDataList.at(i).indexOf(":");
           QString temp = strDataList.at(i).mid(0,index);
           if(temp == "address")
           {
               address = strDataList.at(i).mid(index+1).toInt();
           }
           if(temp == "value")
           {
               value = strDataList.at(i).mid(index+1).toInt();
           }
       }
       body.append(DataTypeConver::intToByteArray(address,2));
       body.append(DataTypeConver::intToByteArray(value,2));

       Package package;
       package.setBody(body);
       package.setFunction(ControlPoint);
       packageList.append(package);
    }
    else if(strDataList.at(num) == "LiftAuthorize")
    {
        body.clear();
        for(int i=1+num; i<=strDataList.size()-1; i++)
        {
            int index = strDataList.at(i).indexOf(":");
            QString temp = strDataList.at(i).mid(0,index);
            if(temp == "householdNumber")
            {
                roomNum = strDataList.at(i).mid(index+1).toULong(0,16);
         //       qDebug()<<roomNum<<" HAHAH";
            }
            else if(temp == "floor")
            {
                floor=strDataList.at(i).mid(index+1);
                qDebug()<<"floor-floor"<<floor;
            }
            else if(temp == "extNumber")
            {
                extNumber = strDataList.at(i).mid(index+1);
                qDebug()<<"extNumber"<<extNumber;
            }


        }
         body.append(DataTypeConver::intToByteArray(roomNum,4));
         body.append(floor.toLatin1());
         body.append((extNumber.toLatin1()));
         Package package;
         package.setBody(body);
         package.setFunction(Elevatorauthorization);
         packageList.append(package);

    }
    else if(strDataList.at(num) == "CallLiftToFirst")
    {
        body.clear();
        qDebug()<<"CallLiftToFirst";
        int index = strDataList.at(6+num).indexOf(":");
        QString temp = strDataList.at(6+num).mid(0,index);
         if(temp.contains("extNumber"))
        {
            doorNum = strDataList.at(6+num).mid(index+1);
            qDebug()<<"CallLiftToFirst extNumber "<<doorNum;
        }

        body.append((doorNum.toLatin1()));
        Package package;
        package.setBody(body);
        package.setFunction(CallLiftToFirst);
        packageList.append(package);
    }
    else if(strDataList.at(num) == "LiftStatusQuery")
    {
        Package package;
        package.setBody(body);
        package.setFunction(ElevatorStatusQuery);
        packageList.append(package);
    }
    else if(strDataList.at(num) == "UpCallLift")
    {
        Package package;
        package.setBody(body);
        package.setFunction(ElevatorUP);
        packageList.append(package);
    }
    else if(strDataList.at(num) == "DownCallLift")
    {
        Package package;
        package.setBody(body);
        package.setFunction(ElevatorDown);
        packageList.append(package);
    }
    }
    for(int num=0;num<data.size();num++)
    {
        quint8 getdata=data.at(num);
        if(getdata==0xEE)
        {
            //qDebug()<<"0xEE now";
            quint8 sum=0;
            quint8 dataLength=data.at(num+1);
            quint8 function=data.at(num+2);
            for(int i=0;i<dataLength;i++)
            {

                sum+=data.at(num+2+i);
                body.append(data.at(num+2+i));
            }
            if(data.at(num+2+dataLength)==(sum&0xFF))
            {
                Package package;
                package.setBody(body);
                package.setFunction(function);
                packageList.append(package);
                //qDebug()<<"SUM GO";
                break;
            }
        }
    }



    return packageList;
}

QByteArray NetPointProtocol::generateData(Package package)
{
    QByteArray data;
    if(package.function()==ControlPoint)
    {
    QString strdata="SetPointValue\r\naddress:\r\nvalue:";
    QString address = QString::number(package.body().mid(0,2).toHex().toInt(0,16));
    QString value   = QString::number(package.body().mid(2,2).toHex().toInt(0,16));

    int startIndex = strdata.indexOf("address")+8;
    strdata.insert(startIndex,address);

    startIndex = strdata.indexOf("value")+6;
    strdata.insert(startIndex,value);

    data = strdata.toLatin1();
    }
    else if(package.function()==Elevatorauthorization)
    {
       data=package.body();
    }
    return data;
}
/*
 *roonNum -- 四位房号 如 0101
floor  -- 门口机所在的楼层
doorDir -- 门口机所在楼层的电梯开门方向 前门或后门
floorDir -- 目的楼层电梯开门方向  前门或后门
brand -- 电梯品牌
 */
QByteArray NetPointProtocol::generateAuthorizeData(quint16 roomNum,quint16 floor,quint8 doorDir,quint8 floorDir,QString brand)
{
    QByteArray data;
     quint8 sum=0;

     if(brand == "4")  // 奥的斯电梯
     {
         int i;

         data[0]=0X40;
         data[1]=0x01;
         data[2]=0xA1;
         data[3]=0x00;
         if(floor==0)
             data[4]=0x01;
         else
            data[4]=0x02;
         data[5]=floorDir;
         data[6]=roomNum>>8;
         data[7]=roomNum&0xff;
         data[8]=doorDir;
         data[9]=floor>>8;
         data[10]=floor&0xff;




         for(i=0;i<=11;i++)
             sum = sum^data[i];


         data[11]=sum;
         data[12]=0X23;

          return data;
     }
     else  if(brand=="2") // 三菱电梯
     {
         int i;
         data[0]=0X02;
         data[1]=0x00;
         data[2]=0x01;
         data[3]=0X06;
         data[4]=0X01;
         data[5]=0X00;
         data[6]=0x80+floorDir*16+doorDir;
         data[7]=floor;
         data[8]=0x00;

         data[9]=roomNum>>8;

         for(i=1;i<=9;i++)
             sum+=data[i];
         sum = sum&0xff;
         sum = ~sum;
         sum+=1;
         data[10]=sum;
         data[11]=0X03;

     }
}


QByteArray NetPointProtocol::generateAuthorizeData(quint16 roomNum,quint8 floor,QString brand)
{
    QByteArray data;
 //   qDebug()<<"RoomNum="<<roomNum;
    quint8 num=((roomNum>>12)*10)+((roomNum>>8) & 0x0f);
//    qDebug()<<"num="<<num;
    quint8 sum=0;
   /* data[0]=0xEE;
    data[1]=0x09;
    data[2]=0x95;
    data[3]=0x01;
    data[4]=(roomNum>>12)+0x30;
    data[5]=((roomNum>>8)&0x0f)+0x30;
    data[6]=((roomNum>>4)&0x0f)+0x30;
    data[7]=(roomNum & 0x0f) +0x30;
    data[8]=num;
    data[9]=floor;
    data[10]=0x01;
    for(int i=2;i<11;i++)
        sum+=data[i];
    data[11]=sum;
    data[12]=0XFE;*/

    if(brand=="2") // 三菱电梯
    {
        int i;
        data[0]=0X02;
        data[1]=0x00;
        data[2]=0x01;
        data[3]=0X06;
        data[4]=0X01;
        data[5]=0X00;
        data[6]=0x80;
        data[7]=floor;
        data[8]=0x00;
        data[9]=roomNum;

        for(i=1;i<=9;i++)
            sum+=data[i];
        sum = sum&0xff;
        sum = ~sum;
        sum+=1;
        data[10]=sum;
        data[11]=0X03;

    }
    else if(brand=="3") // 日立电梯
    {
        int i;
        data[0]=0X02;
        data[1]=0x30;
        data[2]=0x31;
        data[3]=floor/10+0x30;
        data[4]=floor%10+0x30;
        data[5]=0X03;
        data[6]=0x00;
        data[7]=0x00;
        data[8]=0x00;
        data[9]=0x00;

        data[10]=0x00;
        data[11]=0x00;
        data[12]=0x00;
        data[13]=0x00;

        data[14]=(roomNum>>12)+0x30;
        data[15]=((roomNum>>8)&0xf)+0x30;
        data[16]=((roomNum>>4)&0xf)+0x30;
        data[17]=(roomNum&0xf)+0x30;

        for(i=1;i<=17;i++)
            sum+=data[i];
        sum = sum&0xff;

        data[18]=sum;
        data[19]=0X03;

    }
    else
    {

    data[0]=0xEE;
    data[1]=0x95;
    data[2]=0x01;
    data[3]=(roomNum>>12)+0x30;
    data[4]=((roomNum>>8)&0x0f)+0x30;
    data[5]=((roomNum>>4)&0x0f)+0x30;
    data[6]=(roomNum & 0x0f) +0x30;
   // data[7]=num;
    data[7]=floor;
    data[8]=0x01;
    for(int i=2;i<9;i++)
        sum+=data[i];
    data[9]=sum;
    data[10]=0XFE;
    }
    //qDebug()<<data.data();
    return data;

}

QByteArray NetPointProtocol::generateToFirstData(quint8 floorNum)
{
    QByteArray data;
//    qDebug()<<"RoomNum="<<floorNum;
    quint8 sum=0;
    /*data[0]=0xEE;
    data[1]=0x09;
    data[2]=0x94;
    data[3]=floorNum;
    data[4]=0x00;
    data[5]=0x00;
    data[6]=0x00;
    data[7]=0x00;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    for(int i=2;i<11;i++)
        sum+=data[i];
    data[11]=sum;
    data[12]=0XFE;*/

    data[0]=0xEE;
    data[1]=0x94;
    data[2]=floorNum;
    data[3]=0x00;
    data[4]=0x00;
    data[5]=0x00;
    data[6]=0x00;
    data[7]=0x00;
    data[8]=0x00;
    for(int i=2;i<9;i++)
        sum+=data[i];
    data[9]=sum;
    data[10]=0XFE;
    //qDebug()<<data.data();
    return data;
}


QByteArray NetPointProtocol::generateQueryData()
{
    QByteArray data;
    quint8 sum=0;
    data[0]=0xEE;
    data[1]=0x03;
    data[2]=0x90;
    data[3]=0x00;
    data[4]=0x00;
    for(int i=2;i<5;i++)
        sum+=data[i];
    data[5]=sum;
    data[6]=0xfe;
    //qDebug()<<data.data();
    return data;
}

/*QByteArray NetPointProtocol::generateUPDOWMData(quint16 roomNum,int stata)
{
    QByteArray data;
 //   qDebug()<<"roomNum="<<roomNum;
    quint8 sum=0;
    switch(stata)
    {
        case 0: data[2]=0x92;break;
        case 1: data[2]=0x93;break;
    }
    data[0]=0xEE;
    data[1]=0x09;
    data[3]=0x00;
    data[4]=(roomNum>>12)+0x30;
    data[5]=((roomNum>>8)&0x0f)+0x30;
    data[6]=((roomNum>>4)&0x0f)+0x30;
    data[7]=(roomNum & 0x0f) +0x30;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    for(int i=2;i<11;i++)
        sum+=data[i];
    data[11]=sum;
    data[12]=0XFE;
    //qDebug()<<data.data();
    return data;
}*/


QByteArray NetPointProtocol::generateUPDOWMData(quint16 roomNum,int stata,QString brand)
{
    QByteArray data;
 //   qDebug()<<"roomNum="<<roomNum;
    quint8 sum=0;
    if(brand == "0")
    {
    switch(stata)
    {
        case 0: data[2]=0x92;break;
        case 1: data[2]=0x93;break;
    }
    data[0]=0xEE;
    data[1]=0x09;
    data[3]=0x00;
    data[4]=(roomNum>>12)+0x30;
    data[5]=((roomNum>>8)&0x0f)+0x30;
    data[6]=((roomNum>>4)&0x0f)+0x30;
    data[7]=(roomNum & 0x0f) +0x30;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    for(int i=2;i<11;i++)
        sum+=data[i];
    data[11]=sum;
    data[12]=0XFE;
    }
    else if(brand=="1")
    {
        data[0]=0x1A;
        data[1]=0x5A;
        data[2]=0x00;
        data[3]=0XFF;
        data[4]=0X04;
        data[5]=0X02;
        data[6]=0X00;
        data[7]=0x05;
        data[8]=0x02;
        data[9]=0x01;
        data[10]=0X02;
        data[11]=0X01;
        data[12]=0x01;
        data[13]=0xBD;
    }
    else if(brand=="2")
    {
        quint16 sum=0;
        quint8 i;
        data[0]=0x02;
        data[1]=0x00;
        data[2]=0x01;
        data[3]=0X06;
        data[4]=0X01;
        data[5]=0X00;
        data[6]=0X80+stata;

        data[7]=roomNum;


        data[8]=0x00;
        data[9]=0x01;

        for(i=1;i<=9;i++)
            sum+=data[i];
        sum = sum&0xff;
        sum = ~sum;
        sum+=1;
        data[10]=sum;
        data[11]=0X03;

    }
    else if(brand=="3") // 日立电梯
    {
        int i;
        data[0]=0X02;
        data[1]=0x30;
        data[2]=0x31;
        data[3]=0x00;
        data[4]=0x00;
        data[5]=0X02;
        data[6]=0x00;
        data[7]=0x00;
        data[8]=0x00;
        data[9]=0x00;

        data[10]=0x00;
        data[11]=0x00;
        data[12]=0x00;
        data[13]=0x00;

        data[14]=(roomNum>>12)+0x30;
        data[15]=((roomNum>>8)&0xf)+0x30;
        data[16]=((roomNum>>4)&0xf)+0x30;
        data[17]=(roomNum&0xf)+0x30;

        for(i=1;i<=17;i++)
            sum+=data[i];
        sum = sum&0xff;

        data[18]=sum;
        data[19]=0X03;

    }
    else if(brand=="4") // 奥的斯电梯
    {
        int i;

        data[0]=0X40;
        data[1]=0x01;
        data[2]=0xA2;
        data[3]=0x02;
        data[4]=0x00;
        data[5]=stata;
        data[6]=roomNum>>8;
        data[7]=roomNum&0xff;
        data[8]=0x00;
        data[9]=0x00;
        data[10]=0x00;




        for(i=0;i<=11;i++)
            sum = sum^data[i];


        data[11]=sum;
        data[12]=0X23;

    }
    //qDebug()<<data.data();
    return data;
}

